#ifndef COMPOSITION__LISTENER_COMPONENT_HPP_
#define COMPOSITION__LISTENER_COMPONENT_HPP_

#pragma once
#include "DeepDetector.hpp"
#include "base_interfaces/msg/mat_with_time.hpp"
#include "base_interfaces/srv/gimbal_pose.hpp"
#include "base_interfaces/msg/armors.hpp"
#include "base_interfaces/msg/roi.hpp"
#include "base_interfaces/msg/armor.hpp"
#include "base_interfaces/msg/vertice.hpp"
#include <cv_bridge/cv_bridge.hpp>
#include "sensor_msgs/msg/image.hpp"
#include "rclcpp/rclcpp.hpp"
#include "message_filters/subscriber.h"
#include "message_filters/sync_policies/exact_time.h"
#include "base_interfaces/msg/scan_ctrl_info.hpp"


using std::placeholders::_1;
using std::placeholders::_2;
using ROI=cv::Rect2d;
namespace wmj
{
    enum DeepROISizeState
    {
        ROI_BIG = 0,
        ROI_SMALL = 1,
    };
    class DeepDetector_Node : public rclcpp::Node
    {
    public:

        explicit DeepDetector_Node(const rclcpp::NodeOptions & options);

    private:
        // MatWithTime订阅者
        rclcpp::Subscription<sensor_msgs::msg::Image>::SharedPtr imageSub;
        // Roi订阅者
        rclcpp::Subscription<base_interfaces::msg::Roi>::SharedPtr roiSub;
        // Armors发布者
        rclcpp::Publisher<base_interfaces::msg::Armors>::SharedPtr armorsPub;

        rclcpp::Subscription<base_interfaces::msg::ScanCtrlInfo>::SharedPtr ScanmodeSub;    //接收控制信息

        //双目同步推理（已弃用）  
        // message_filters::Subscriber<sensor_msgs::msg::Image> left_image_sub_;
        // message_filters::Subscriber<sensor_msgs::msg::Image> right_image_sub_;
        // typedef message_filters::sync_policies::ExactTime<sensor_msgs::msg::Image, sensor_msgs::msg::Image> MySyncPolicy;
        // std::shared_ptr<message_filters::Synchronizer<MySyncPolicy>> sync_;
        
        std::shared_ptr<wmj::DeepDetector>  m_deepDetector;         // DeepDetector实例
        ROI m_roi;                    // Roi区域，用于储存libControl传过来的数据 
        ROI m_camera_resolution;
        cv::Size2i m_deep_roi_size;
        ROI m_deep_default_roi;  
        DeepROISizeState m_deep_roi_state;
        bool roi_init_;
        int m_track_lost_cnt; // 丢识别计数
        bool m_isscan = 1;

        std::string model_name_;               

        /**
         * @brief MatWithTime回调函数
         * 
         * @param msg MatWithTime智能指针
        */
        void imageCallBack(const sensor_msgs::msg::Image::ConstSharedPtr& msg);
                
        //双目同步推理（已弃用）  
        // void sync_images_callback_(
        //     const sensor_msgs::msg::Image::ConstSharedPtr& left_image,
        //     const sensor_msgs::msg::Image::ConstSharedPtr& right_image);


        /**
         * @brief Roi回调函数
         * 
         * @param msg Roi智能指针
        */
        void roiCallBack(const base_interfaces::msg::Roi::SharedPtr msg); 
        
        /**
         * @brief 图像获取函数,将sensor类型转换为Mat类型，需要依赖cv_bridge
         * 
         * @param img sensor_msgs::Image类型图像
        */
        static cv::Mat getImg(const sensor_msgs::msg::Image::ConstSharedPtr& img);

        void scanCtrlInfoCallback(const base_interfaces::msg::ScanCtrlInfo::SharedPtr msg);

        
        /**
         * @brief 读ROS参数
         */
        void getParams(DetectorParams &params);

        /**
         * @brief 计算ROI
         * 
         * @param armors 
         */
        void setROI(const Armors &armors);
        void setDeepRoiSize(cv::Rect2d camera_resolution);

    };

  
}

#endif
